import { defineStore } from "pinia"; // 定义容器
import { ref, computed, reactive } from 'vue';
import { getAnswerList } from '@/api/answer.js';
import { showLoadingToast } from 'vant';


const ccc = {
  "code": 200,
  "msg": "Success",
  "data": {
    "list": [
      {
        "cardId": "1787314150403055617",
        "topic": "嗯哼",
        "question": "你说是就是咯1",
        "topicType": "单选题",
        "score": "2分",
        "answer": "A",
        "options": "A:对不咯B:就是咯",
        "title": "测试视频一",
        "optionList": [
          {
            "name": "A",
            "topic": "对不咯"
          },
          {
            "name": "B",
            "topic": "就是咯"
          }
        ]
      },
      {
        "cardId": "1787314150403055617",
        "topic": "嗯哼",
        "question": "你说是就是咯2",
        "topicType": "单选题",
        "score": "2分",
        "answer": "A",
        "options": "A:对不咯B:就是咯",
        "title": "测试视频一",
        "optionList": [
          {
            "name": "A",
            "topic": "对不咯"
          },
          {
            "name": "B",
            "topic": "就是咯"
          }
        ]
      },
      {
        "cardId": "1787314150403055617",
        "topic": "嗯哼",
        "question": "你说是就是咯3",
        "topicType": "单选题",
        "score": "2分",
        "answer": "A",
        "options": "A:对不咯B:就是咯",
        "title": "测试视频一",
        "optionList": [
          {
            "name": "A",
            "topic": "对不咯"
          },
          {
            "name": "B",
            "topic": "就是咯"
          }
        ]
      }
    ],
    "pagination": {
      "currentPage": 1,
      "pageSize": 220,
      "total": 1
    }
  }
}


//  获取答题列表，并对其进行缓存
export const useAnswerList = defineStore('answerList', () => {
  //  data
  const answerList = ref([]);
  let answerListLength = ref(0);

  //  methods
  async function setAnswerList(videoId) {
    const { code, data } = await getAnswerList(videoId);
  // const { code, data } = ccc;
    if(code === 200) {
      answerList.value = data.list.map(_s => {
        _s.answerArray = [];
        _s.userAnswer = '';
        return _s;
      })
    }

    answerListLength.value = answerList.value.length;

    return answerList.value;
  }

  /**
   * 更新数据中的 选中项, 更新 optionList 里面的数据
   * @param listIndex 当前是第几题
   * @param answerArray 选择的答案集合
   * */
  function modifyAnswerList(listIndex, answerArray) {
    //  修改指定的题目记录答案，便于回显
    answerList.value[listIndex].answerArray = answerArray;
    answerList.value[listIndex].userAnswer = answerArray.join('');
  }

  function  clearAnswerList() {
    answerList.value = [];
  }

  return { answerList, answerListLength, setAnswerList, modifyAnswerList, clearAnswerList }
})

//  处理当前选中的答案
export const useAnswer = defineStore('answer', () => {

})

//  记录当前播放的视频的信息，便于后期跳转回当前的播放页面
//  标题名称
export const useCurrentVideo = defineStore('currentVideo', () => {
  //  data
  const currentVideo = ref({ path: "/Home" });
  const currentVideoId = ref('');

  //  methods
  function setCurrentVideo(val) {
    currentVideo.value = val;
  }

  function setCurrentVideoId(videoId) {
    currentVideoId.value = videoId;
  }

  return { currentVideo, currentVideoId, setCurrentVideo, setCurrentVideoId }
})

